<?
include("./_common.php");
include("$g4[path]/lib/shop.lib.php");


$order_row = mysql_fetch_array(mysql_query("select * from g4_order where oid = '".$_SESSION["g4_oid"]."' limit 1"));

if($order_row[mb_part] == "member")
{
	if($order_row[pay_point] > 0)
	{
		$point_sql = "insert into g4_point set ";
		$point_sql.= "mb_id = '".$order_row[mb_id]."', ";
		$point_sql.= "po_datetime = now(), ";
		$point_sql.= "po_content = '주문번호 [".$order_row[oid]."]건 결제 취소', ";
		$point_sql.= "po_point = '".$order_row[pay_point]."', ";
		$point_sql.= "po_rel_table = '@shop', ";
		$point_sql.= "po_rel_id = 'system', ";
		$point_sql.= "po_rel_action = '".date("Y-m-d")."' ";

		$point_rst = mysql_query($point_sql);

		if($point_rst)
		{
			$member_sql = "update g4_member set mb_point = mb_point + ".$order_row[pay_point]." where mb_id = '".$order_row[mb_id]."' limit 1";
			$member_rst = mysql_query($member_sql);
		}
	}
}

$order_goods_sql = "select * from g4_order_goods where oid = '".$order_row[oid]."' ";
$order_goods_rst = mysql_query($order_goods_sql);
while($order_goods_row = mysql_fetch_array($order_goods_rst))
{
	$goods_update_sql = "update g4_goods set g_cnt = g_cnt + ".$order_goods_row[cnt].", g_sale = g_sale - ".$order_goods_row[cnt]." where g_code='".$order_goods_row[g_code]."' limit 1";

	$goods_update_rst = mysql_query($goods_update_sql);

	if($goods_update_rst)
	{
		mysql_query("delete from g4_order_goods where idx='".$order_goods_row[idx]."' limit 1");
	}
}

$sql = "delete from g4_order where oid='".$order_row[oid]."' limit 1";
$rst = mysql_query($sql);

if($rst)
{
	echo("<script>alert('취소 되었습니다.');location.href='goods_list.php';</script>");
}
else
{
	echo("<script>alert('취소실패!! 관리자에게 문의하세요');history.back(-1);</script>");
}
?>